package com.example.learningnote;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import androidx.appcompat.app.AppCompatActivity;


public class CustomViewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_view);
        LinearLayout mainContainer = findViewById(R.id.mainContainer);
        // 控制需要生成几个 title_with_content
        int numberOfTitles = 3; // 假设我们需要 3 个 title_with_content
        LayoutInflater inflater = LayoutInflater.from(this);

        for (int i = 0; i < numberOfTitles; i++) {
            // 动态加载 title_with_content 布局
            View titleLayout = inflater.inflate(R.layout.title_with_content, mainContainer, false);
            // 添加到父容器中
            mainContainer.addView(titleLayout);
            // 找到子视图并设置属性
            TextView titleTextView = titleLayout.findViewById(R.id.titleTextView);
            FrameLayout contentContainer = titleLayout.findViewById(R.id.contentContainer);
            // 设置每个 title_with_content 的内容
            if (i == 0) {
                titleTextView.setText("横向滚动");
                setupHorizontalScrollView(contentContainer);
            } else if (i == 1) {
                titleTextView.setText("其他内容");
                setupOtherContent(contentContainer);
            } else {
                titleTextView.setText("第三个标题");
                setupOtherContent(contentContainer);
            }
        }
    }



    private void setupHorizontalScrollView(FrameLayout contentContainer) {
        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
        horizontalScrollView.setHorizontalScrollBarEnabled(false);
        horizontalScrollView.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));

        LinearLayout imageViewsLayout = new LinearLayout(this);
        imageViewsLayout.setOrientation(LinearLayout.VERTICAL);
        imageViewsLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        for (int row = 0; row < 2; row++) {
            LinearLayout rowLayout = new LinearLayout(this);
            rowLayout.setOrientation(LinearLayout.HORIZONTAL);
            rowLayout.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            for (int i = 0; i < 10; i++) {
                ImageView imageView = new ImageView(this);
                imageView.setLayoutParams(new LinearLayout.LayoutParams(240, 240));
                imageView.setPadding(10, 10, 10, 10);

                Glide.with(this)
                        .load(R.drawable.test)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(16)))
                        .into(imageView);

                rowLayout.addView(imageView);
            }
            imageViewsLayout.addView(rowLayout);
        }

        horizontalScrollView.addView(imageViewsLayout);
        contentContainer.addView(horizontalScrollView);
    }

    private void setupOtherContent(FrameLayout contentContainer) {
        // 这里可以定义第二个 title_with_content 需要显示的内容
        TextView textView = new TextView(this);
        textView.setText("这里是其他内容");
        textView.setTextSize(16);
        textView.setPadding(20, 20, 20, 20);
        contentContainer.addView(textView);
    }
}
